/*
 * Potion Colors:
 *  Heal(1) - Red (255,0,0)
 *  Envenom(2) - Green (10,250,10)
 *  PowerUp(3) - Orange (255,128,0)
 *
 * Ingredent Colors:
 *  Water(1) - Light Blue (47,230,234)
 *  Eye of Newt(2) - Purple (128,0,128)
 *  Dragon Scale(3) - Yellow (255,255,0)
 *  Unicorn Blood(4) - Light Yellow (255,255,55)
 *
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <p18F26K80.h>
#include "IR_Send.h"

#define SIN	LATAbits.LATA0
#define SCLK	LATAbits.LATA1
#define XLAT	LATAbits.LATA2
#define BLANK	LATAbits.LATA3

#define MAX_STEP 4095

int a,b,c,d;
unsigned int GSData[24];

void setLED_potion(int potionNumber);
void setLED_ing(int ledNumber, int ingredNumber);
void setLED(int lednum, int r, int g, int b);
void startTLC(void);
void delay(unsigned int cycles);
void resetLED(void);
void setupPIC (void);
void maxLED(void);
void writeLED( void );
void setPWM(int chan, int pwm);
void fade_on(int lednum, int r, int g, int b, int fadeSpeed);
void all_off(void);
void place_potion(void);
void blow_up(void);



void maxLED(void){
    for(d = 0; d < 23;d++){
        GSData[d] = 255;
    }
}

void resetLED(void){
    for(d = 0; d < 24;d++){
        GSData[d] = 0;
    }
}

void startTLC(void){
    SCLK = 0;
    SIN = 0;
    XLAT = 0;
    BLANK = 1;
}

void all_off(void){
    resetLED();
    writeLED();
}

void place_potion(void){
    setLED(6,255,255,0);
    writeLED();
}

void writeLED(void){
    XLAT = 0;
    for (c = 23; c >= 0; c--)
    {
        for (b = 11; b >= 0; b--)
        {
            SCLK = 0;
            delay(2);
            if ( GSData[c] & (1 << b) )
                SIN = 1;
            else
                SIN = 0;
            SCLK = 1;
        }
    }
    SCLK = 0;

    BLANK = 1;
    XLAT = 1;
    BLANK = 0;
    XLAT = 0;
}

void setLED(int lednum, int r, int g, int b)
{
    setPWM(lednum*3, r);
    setPWM(lednum*3+1, g);
    setPWM(lednum*3+2, b);
}

void setPWM(int chan, int pwm)
{
    if (pwm > MAX_STEP) pwm = 255;
    if (chan > 24) return;

    GSData[chan] = pwm;
}


void setLED_ing(int ledNumber, int ingredNumber){
    switch(ingredNumber){
        case 1: //Water
            fade_on(ledNumber,47,230,234,100);
            break;
        case 2: //Eye of Newt
            fade_on(ledNumber,128,0,128,100);
            break;
        case 3: //Dragon Scale
            fade_on(ledNumber,255,255,0,100);
            break;
        case 4: //Unicorn Blood
            fade_on(ledNumber,255,255,55,100);
            break;
        case 5: // Power On
            fade_on(ledNumber, 0,255,0, 100);
            break;
        default:
            all_off();
            break;
    }
}

void setLED_potion(int potionNumber){
    switch(potionNumber){
        case 1: //Heal
            setLED(1,255,0,0);
            writeLED();
            break;
        case 2: //Envenom
            setLED(1,10,250,10);
            writeLED();
            break;
        case 3: //Power Up
            setLED(1,255,128,0);
            writeLED();
            break;
        case 4: //EXPLOSION
            blow_up();
            //Sound
            break;
        default:
            all_off();
            break;
    }
}

void fade_on(int lednum, int r, int g, int b, int fadeSpeed){
    for(a = fadeSpeed; a > 0; a=a-4){
        setLED(lednum, r/a, g/a, b/a);
        delay(100);
        writeLED();
    }
}

void blow_up(void){
    int start = 100;
    for(a = 0; a < 25; a++){
        start += 2;
        setLED(0,start,0,0);
        setLED(1,start,0,0);
        setLED(2,start,0,0);
        setLED(6,start,0,0);
        setLED(7,start,0,0);
        writeLED();
        if(start%4){
            //Send Damage
            IR_Send(4);
        }else{
        delay(20000);
        }
        all_off();
        delay(20000);
    }
}
